var organization =new Ext.Panel( {
	id : 'orgnization-panel',
	title : '组织视图',
	layout : 'border',
	defaults : {
		collapsible : true,
		bodyStyle : 'padding:15px'
	},
	items : [ {
		title : '组织模型',
		region : 'center',
		autoScroll : true,
		border : false,
		contentEl : 'organizationCanvas'
	} ]
});


var companyImg = new Image();
companyImg.src = 'images/modelUnit/department/company.jpg';
var departmentImg = new Image();
departmentImg.src = 'images/modelUnit/department/department.jpg';

nodeWidth=100;		//节点宽度
layerHeight=100;		//每层树的高度
var nodes=new Array();		//所有节点数组
var companynodes=new Array();		//指定公司的节点数组
var company;			//公司节点

function showDepartmentContent(cid,text) {
	var departmentStore = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : 'showDepartmentGraph.action?method=showDepartmentGraph',
			method : 'post'

		}),
		reader : new Ext.data.JsonReader({
			root : 'Department',
			totalProperty : 'totalDepartment',
			id : 'uid'
		}, [ {
			name : 'cnName'
		}, {
			name : 'enName'
		}, {
			name : 'fileId'
		}, {
			name : 'groupId'
		},{
			name : 'id'
		},{
			name : 'parentName'
		},{
			name : 'uid'
		}])
	});

	departmentStore.load({
		 params:{
				fileId : cid
			},
		callback : function(r, options, success) {
			if (success == false) {
				alert("加载数据失败，无对应数据或者系统出现异常！");
			} else {
				var canvas = document.getElementById('organizationCanvas');
				var ctx = canvas.getContext('2d');
				ctx.clearRect(0, 0, canvas.width, canvas.height);
				nodes=new Array();
				companynodes=new Array();
				
				buildTree(departmentStore,text);
				calcNodePosition(company);				
				drawModel(ctx);				
				
				// drawDomainModel(300,100,ctx,attributeStore,stateStore,text);
//				drawCompanyModel(400, 20, ctx, departmentStore);
//				drawDepartmentModel(0, 150, ctx, departmentStore);
				
			}
		}
	});
}

//**绘制组织视图**//

//组织节点对象
function Node(cnName,enName,parent){
	this.enName=enName;				
	this.cnName=cnName;
	this.parent=parent;
	this.x=0;						//x坐标
	this.y=0;						//y坐标
	this.children=new Array();		//子节点
	this.treeWidth=0;					//以此节点为根的子树宽度
	
	//计算以此节点为根的树的宽度
	this.calcTreeWidth=function(){
		if(this.children.length==0){
			if(this.parent=='root'){
				this.treeWidth=nodeWidth;
			}else{
				this.treeWidth=nodeWidth;
			}
		}else{
			this.treeWidth=0;
			for(var i=0;i<this.children.length;i++){
				this.children[i].calcTreeWidth();
				this.treeWidth+=this.children[i].treeWidth;
			}
		}
	};
};

//读取数据，构建节点树
function buildTree(departmentStore,name){
	for (var i = 0; i < departmentStore.getCount(); i++) {
		var record = departmentStore.getAt(i);
		var n=new Node(record.get('cnName'),record.get('id'),record.get('parentName'));
		//alert(n.toString());
		nodes.push(n);		
		if(n.parent=="root" && n.cnName==name){
			company=n;
			//alert(company.toString());
		}
	}
	
	for (var i = 0; i < nodes.length; i++) {		
		for(var j=0;j<nodes.length;j++){
			if(nodes[i].parent==nodes[j].enName){
				nodes[j].children.push(nodes[i]);
				break;
			}
		}
	}
	
	companyArray(company);
	
	//计算每个节点子树宽度		
	company.calcTreeWidth();	
	
	//设置每棵树根节点坐标
	company.x=20;
	company.y=20;
}

//计算指定节点的子树中每个节点的坐标
function calcNodePosition(com){
	var c=com.children;
	for(var i=0;i<c.length;i++){
		if(i==0){
			c[i].x=com.x;
			c[i].y=com.y+layerHeight;
		}else{
			c[i].x=c[i-1].x+c[i-1].treeWidth;
			c[i].y=c[i-1].y;
		}
		calcNodePosition(c[i]);
	}
}

//构建指定公司的节点数组
function companyArray(company){
	companynodes.push(company);
	for(var i=0;i<company.children.length;i++){
		companyArray(company.children[i]);
	}
}

//绘图
function drawModel(ctx){
	
	//companyImg.load();
	
	//departmentImg.load();
	for(var i=0;i<companynodes.length;i++){
		if(companynodes[i].parent=='root'){			
			ctx.drawImage(companyImg, companynodes[i].x, companynodes[i].y,companyImg.width,companyImg.height);			
		}else{			
			ctx.drawImage(departmentImg, companynodes[i].x, companynodes[i].y,departmentImg.width,departmentImg.height);						
		}		
		for(var j=0;j<companynodes[i].children.length;j++){			
			drawCtoPLine(ctx,companynodes[i].x+departmentImg.width/2,companynodes[i].y+departmentImg.height+15,companynodes[i].children[j].x+departmentImg.width/2,companynodes[i].children[j].y);
		}	
		ctx.font = "10pt Arial";
		ctx.textBaseline = "top"; // 设置文字垂直对齐方式
		ctx.textAlign = "center"; // 设置文字水平对齐方式
		ctx.fillText(companynodes[i].cnName, companynodes[i].x+departmentImg.width/2,companynodes[i].y+departmentImg.height);
	}
}

//画直线
function drawCtoPLine(ctx,x,y,movetox,movetoy){
	 ctx.beginPath();
	 ctx.moveTo(x,y);
	 ctx.lineTo(movetox,movetoy);
	 ctx.stroke();
}

//旧代码/////////////////
/*
function drawCompanyModel(startx, starty, ctx,departmentStore) {
	var companyImg = new Image();
	companyImg.onload = function() {
		for (var i = 0; i < departmentStore.getCount(); i++) {
			var record = departmentStore.getAt(i);
			if(record.get('parentName')=="root"){
				ctx.drawImage(companyImg, startx, starty);
				ctx.font = "10pt Arial";
				ctx.fillText(record.get('enName'), startx + 10, starty + 60);
				starty=starty+500;
			}
		}
	}
	companyImg.src = 'images/modelUnit/department/company.jpg';

}

function drawDepartmentModel(startx, starty, ctx, departmentStore) {

	var departmentImg = new Image();
	departmentImg.onload = function() {
		for (var i = 0; i < departmentStore.getCount(); i++) {
			var record = departmentStore.getAt(i);
			ctx.drawImage(departmentImg, startx + i * 100, starty);
			ctx.font = "10pt Arial";
			ctx.fillText(record.get('enName'), startx + 5 + i * 100, starty+ 60);
			drawCtoPLine(415,80,ctx,startx+20+i*100,starty);				

		}

	}
	departmentImg.src = 'images/modelUnit/department/department.jpg';

}
function drawCtoPLine(x,y,ctx,movetox,movetoy){
	 ctx.beginPath();
	 ctx.moveTo(x,y);
	 ctx.lineTo(movetox,movetoy);
	 ctx.stroke();
}
*/